$(function () {
    var lastIndex;
    var depts;
    $.get("/department/listAll.do", function (data, status) {
        depts = data;
        $("#hrms_user_list").datagrid({
            title:"用户管理",
            iconCls:"icon-tip",
            width:"auto",
            height:"auto",
            nowrap:false,
            striped:true,
            fit:true,
            url:"/user/list.do",
            method:"get",
            sortName:"id",
            sortOrder:"desc",
            idField:"id",
//            singleSelect:true,
            columns:[
                [
                    {title:"序号", field:"id", width:80, editor:"text"},
                    {field:'ck',checkbox:true},
                    {title:"登录名", field:"userName", width:150, align:"center", sortable:true, editor:"text"},
                    {title:"登陆密码", field:"userPass", width:150, align:"center", sortable:true, formatter:function(val,rec){
                        return "******";
                    }},
                    {title:"真实姓名", field:"realName", width:150, align:"center", sortable:true, editor:"text"},
                    {title:"所属部门", field:"deptId", width:150, align:"center", sortable:true, formatter:function(val,rec){
                        var _dept = rec.department;
                        if(_dept)
                            return _dept.name;
                        return "";
                    },editor:{type:"combobox",options:{valueField:"id",textField:"name",data:depts,required:true}}}
                ]
            ],
            pagination:true,
            rownumbers:true,
            toolbar:[
                {
                    text:"增加",
                    iconCls:"icon-add",
                    handler:function () {
                        $("#hrms_user_list").datagrid("endEdit", lastIndex);
                        $("#hrms_user_list").datagrid("appendRow", {
                            id:"",
                            userName:"",
                            userPass:"123456",
                            realName:""
                        });
                        lastIndex = $("#hrms_user_list").datagrid("getRows").length - 1;
                        $("#hrms_user_list").datagrid("selectRow", lastIndex);
                        $("#hrms_user_list").datagrid("beginEdit", lastIndex);
                    }
                },
                "-",
                {
                    text:"删除",
                    iconCls:"icon-remove",
                    handler:function () {
                        var rows = $('#hrms_user_list').datagrid('getSelections');
                        for(var i=rows.length-1;i>=0;i--){
                            var index = $("#hrms_user_list").datagrid("getRowIndex", rows[i]);
                            $("#hrms_user_list").datagrid("deleteRow", index);
                        }
                    }
                },
                "-",
                {
                    text:"保存修改",
                    iconCls:"icon-save",
                    handler:function () {
                        $("#hrms_user_list").datagrid("endEdit", lastIndex);
                        var addRows = $("#hrms_user_list").datagrid("getChanges", "inserted");
                        var delRows = $("#hrms_user_list").datagrid("getChanges", "deleted");
                        var updateRows = $("#hrms_user_list").datagrid("getChanges", "updated");
                        var reload = false;
                        for (var i = 0; i < addRows.length; i++) {
                            reload = true;
                            $.ajax({
                                url:"/user/add.do",
                                type:"post",
                                data:{
                                    userName:addRows[i].userName,
                                    userPass:addRows[i].userPass,
                                    realName:addRows[i].realName,
                                    deptId:addRows[i].deptId
                                }
                            });
                        }
                        for (var i = 0; i < delRows.length; i++) {
                            reload = true;
                            $.ajax({
                                url:"/user/" + delRows[i].id + ".do",
                                type:"delete"
                            });
                        }
                        for (var i = 0; i < updateRows.length; i++) {
                            $.ajax({
                                url:"/user/" + updateRows[i].id + ".do",
                                type:"post",
                                data:{
                                    id:updateRows[i].id,
                                    userName:updateRows[i].userName,
                                    userPass:updateRows[i].userPass,
                                    realName:updateRows[i].realName,
                                    deptId:updateRows[i].deptId
                                }
                            });
                        }
                        $("#hrms_user_list").datagrid("acceptChanges");
                        showTips("提示消息", "保存成功！");
                        if (reload)
                            $("#hrms_user_list").datagrid("reload");
                    }
                },
                "-",
                {
                    text:"撤销修改",
                    iconCls:"icon-undo",
                    handler:function () {
                        $("#hrms_user_list").datagrid("rejectChanges");
                    }
                }
            ],
            onClickRow:function (rowIndex) {
                if (lastIndex != rowIndex) {
                    $("#hrms_user_list").datagrid("endEdit", lastIndex);
                }
            },
            onDblClickRow:function (rowIndex) {
                if (lastIndex != rowIndex) {
                    $("#hrms_user_list").datagrid("endEdit", lastIndex);
                    $("#hrms_user_list").datagrid("beginEdit", rowIndex);
                }
                lastIndex = rowIndex;
            },
            onBeforeLoad:function () {
                $(this).datagrid("rejectChanges");
            }
        });
        $('#hrms_user_list').datagrid('hideColumn', "id");
    });
});